//
//  StatusCell.swift
//  Status
//
//  Created by 花轮 on 15/5/18.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit
import SDWebImage

protocol StatusCellDelegate: NSObjectProtocol {
    func statusCellDidSelectedImg(cell: StatusCell, index: Int)
}

class StatusCell: UITableViewCell {
    
    weak var imgDelegate: StatusCellDelegate?
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var reContentLabel: UILabel!

    @IBOutlet weak var collectionView: UICollectionView!
    // 图片的宽高
    @IBOutlet weak var widthContraint: NSLayoutConstraint!
    @IBOutlet weak var heightContraint: NSLayoutConstraint!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var bottomView: UIView!
    
    
    @IBOutlet weak var renzhengIcon: UIImageView!
    @IBOutlet weak var vipIcon: UIImageView!
    
    
    var status: Status?
        {
        didSet{
            nameLabel.text = status?.user?.name
            timeLabel.text = status?.created_at
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            iconView.sd_setImageWithURL(status?.user!.iconURL)
            renzhengIcon.image = status?.user?.renzhengImage
            vipIcon.image = status?.user?.vipImage
            
//            println(calcPictureViewSize(status!))
            let pSize = calcPictureViewSize(status!)
            widthContraint.constant = pSize.viewSize.width
            heightContraint.constant = pSize.viewSize.height
            
            layout.itemSize = pSize.itemSize
            
            collectionView.reloadData()
            
            reContentLabel?.text = (status?.retweeted_status?.user?.name ?? "") + ":" + (status?.retweeted_status?.text ?? "")
        }
    }
    
    class func cellReuseID(status: Status) -> String {
        if status.retweeted_status != nil{
            return "reCell"
        }
        return "homeCell"
    }
    
    func rowHeight(status: Status) -> CGFloat{
        self.status = status
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    private func calcPictureViewSize(status: Status) -> (viewSize:CGSize, itemSize:CGSize){
        let s: CGFloat = 90
        let itemSize = CGSizeMake(s, s)
        
        let m: CGFloat = 10
        
        let imageCount = status.pictureURLs?.count ?? 0
        
        // 没有图片
        if imageCount == 0
        {
            return (CGSizeZero, itemSize)
        }
        
        if imageCount == 1
        {
            // 利用SDWebImage从缓存加载图像
            let key = status.pictureURLs![0].absoluteString
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key)
            
//            println("单张图片\(image.size)")
            return (image.size, image.size)
        }
        
        // 4张图
        if imageCount == 4
        {
            return(CGSizeMake(s*2+m, s*2+m), itemSize)
        }
        
//        println("多张\(imageCount)")
        let row = CGFloat((imageCount - 1) / 3 + 1)
        
        return (CGSizeMake(s*3+m*2, row*s+(row-1)*m), itemSize)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 16
        reContentLabel?.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 16
    }
}

// 处理collectionview的相关事件
extension StatusCell: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//        println("选中 \(indexPath)  \(status?.largePicURLs![indexPath.item])")
        imgDelegate?.statusCellDidSelectedImg(self, index: indexPath.item)
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pictureURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("pictureCell", forIndexPath: indexPath) as! PictureCell
        
        cell.url = status!.pictureURLs![indexPath.item]
        
        return cell
    }
}

class PictureCell: UICollectionViewCell {
    
    @IBOutlet weak var iconView: UIImageView!
    
    var url: NSURL?{
        didSet{
            iconView.sd_setImageWithURL(url)
        }
    }
}
